﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace MeterReader.Views
{
    /// <summary>
    /// MetersDataMonitorView.xaml 的交互逻辑
    /// </summary>
    public partial class MetersDataMonitorView : UserControl
    {
        private DispatcherTimer? _timer;

        public MetersDataMonitorView()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateTimeText();
            _timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) };
            _timer.Tick += CurrentTime;
            _timer.Start();
        }

        private void CurrentTime(object? sender, EventArgs e) => UpdateTimeText();

        private void UpdateTimeText()
        {
            timeStr.Text = DateTime.Now.ToString("HH:mm");
            dateStr.Text = DateTime.Now.ToString("yyyy-MM-dd");
            weekStr.Text = GetWeekStr();
        }

        private string GetWeekStr()
        {
            int index = (int)DateTime.Now.DayOfWeek;

            string[] week = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

            return week[index];
        }
    }
}